package org.itboys.mobile.service.mongo.web.cookbook;

import org.apache.commons.lang.StringUtils;
import org.itboys.admin.service.lasted.SundayUploadService;
import org.itboys.commons.CommonConstants;
import org.itboys.commons.utils.tools.ListUtil;
import org.itboys.commons.utils.tools.LongUtil;
import org.itboys.framework.resource.ResourceHolder;

import org.itboys.mobile.entity.mongo.cookbook.KitchenCookBook;
import org.itboys.mongodb.core.MongoDataSource;
import org.itboys.mongodb.service.BaseMongoService;
import org.itboys.param.PageResult;
import org.itboys.param.ParamUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 作者：zzq
 * 时间：2017年5月9日
 * 描述：花果山（农厨）_菜谱_service
 */
@Service
public class KitchenCookBookService extends BaseMongoService<KitchenCookBook> {
    @Resource(name = "kitchenDs")
    private MongoDataSource kitchenDataSource;
    @Autowired
    private SundayUploadService uploadService;
    @Autowired
    protected ResourceHolder resourceHolder;
    @Autowired
    private KitchenCookBookProductService bookProductService;
    @Override
    protected MongoDataSource getMongoDataSource() {
        return kitchenDataSource;
    }
    @Override
    protected Class<KitchenCookBook> getEntityClass() {
        return KitchenCookBook.class;
    }

    /**
     * 分页查询菜谱
     * @param request
     * @return
     */
    public PageResult<KitchenCookBook> selectCookBook(HttpServletRequest request){
        Map<String,Object> param =new HashMap<String, Object>();
        param.put("isDeleted", CommonConstants.NO);

        Map<String,String> containsparam = ParamUtil.packageMongoVagueParam(request,"name","viceName");
        PageResult<KitchenCookBook>bookPageResult=super.containpageQuery(request,containsparam,param);

        return bookPageResult;
    }
    /**
     * 查找单一菜谱实体
     * @param cookBookId
     * @return
     */
    public KitchenCookBook findOne(Long cookBookId){
        KitchenCookBook book=null;

        if(LongUtil.isNotZreo(cookBookId)){
            book=super.getById(cookBookId);
        }else{
            book=new KitchenCookBook();
            book.setId(0);

        }
        return book;
    }
    /**
     * 新增或修改菜谱
     * @param cookBook
     * @param imageFile
     * @param videoFile
     * @throws IOException
     */
    public void saveCookBook(KitchenCookBook cookBook, MultipartFile imageFile,MultipartFile videoFile) throws IOException {
        Map<String, String> map = uploadService.uploadSingle(imageFile);
        if(StringUtils.isNotEmpty(map.get(CommonConstants.UPLOAD_HALF_PATH))){
            cookBook.setImage(map.get(CommonConstants.UPLOAD_HALF_PATH));
        }
        Map<String, String> videoFileMap = uploadService.uploadSingle(videoFile);
        if(StringUtils.isNotEmpty(videoFileMap.get(CommonConstants.UPLOAD_HALF_PATH))){
            cookBook.setVideoUrl(videoFileMap.get(CommonConstants.UPLOAD_HALF_PATH));
        }

        if(cookBook.getId()==0l){
            super.save(cookBook);
        }else{
            super.updateExceptEmpty(cookBook.getId(), cookBook);

        }
    }

    /**
     * 设置为推荐
     * @param id
     * @param status
     * @param type 1，首页推荐。2其他待定
     */
    public void setRecomment(Long id,Integer status,Integer type){

        if(type==1){
            super.update(id,"isIndexRecommend",status);
        }

    }
}
